package com.gxuwz.weather.config;

import android.util.Log;

import com.gxuwz.weather.model.WeatherDay2and15;
import com.gxuwz.weather.model.Weatherhour3;
import com.gxuwz.weather.model.Weathernow;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class JsonRead {
    private List<Weathernow> weatherList = new ArrayList<>();

    public List<Weathernow> getWeatherList() {
        return weatherList;
    }

    public void updateWeatherData(String weatherData) {
        try {
            JSONObject jsonResponse = new JSONObject(weatherData);
            JSONObject data = jsonResponse.getJSONObject("data");
            JSONObject now = data.getJSONObject("now");
            JSONObject lifeIndex = data.getJSONObject("lifeIndex");
            JSONArray alarmList = data.getJSONArray("alarmList");
            JSONArray hour3data = data.getJSONArray("hour3data");

            List<Weatherhour3> hour3List = new ArrayList<>();
            for (int i = 0; i < hour3data.length(); i++) {
                String[] parts = hour3data.getString(i).split(",");
                hour3List.add(new Weatherhour3(parts[0], parts[1], parts[3]));
            }

            // 调用7d和15d的请求方法
            String areaCn = now.getString("cityname");
            String weatherData7 = RequestDemo7.getWeatherData7(areaCn);
            String weatherData15 = RequestDemo15.getWeatherData15(areaCn);

            // 解析7d返回的JSON数据
            List<WeatherDay2and15> day2and15List = new ArrayList<>();
            JSONObject jsonResponse7 = new JSONObject(weatherData7);
            JSONObject data7 = jsonResponse7.getJSONObject("data");
            for (int i = 2; i <= 7; i++) {
                String key = "d" + i;
                JSONObject dayData = data7.getJSONObject(key);
                WeatherDay2and15 weatherDay2and15 = new WeatherDay2and15(
                        dayData.getString("temperature_min"),
                        dayData.getString("temperature_max"),
                        dayData.getString("wind_day"),
                        dayData.getString("wind_pow"),
                        dayData.getString("time"),
                        dayData.getString("weather"),
                        dayData.getString("day_weather_pic")
                );
                day2and15List.add(weatherDay2and15);
            }

            // 解析15d返回的JSON数据
            JSONObject jsonResponse15 = new JSONObject(weatherData15);
            JSONObject data15 = jsonResponse15.getJSONObject("data");
            for (int i = 8; i <= 15; i++) {
                String key = "d" + i;
                JSONObject dayData = data15.getJSONObject(key);
                WeatherDay2and15 weatherDay2and15 = new WeatherDay2and15(
                        dayData.getString("temperature_min"),
                        dayData.getString("temperature_max"),
                        dayData.getString("wind"),
                        dayData.getString("wind_pow"),
                        dayData.getString("time"),
                        dayData.getString("weather"),
                        dayData.getString("day_weather_pic")
                );
                day2and15List.add(weatherDay2and15);
            }

            String issueContent = "无预警信息";
            if (alarmList.length() > 0) {
                JSONObject firstAlarm = alarmList.getJSONObject(0);
                issueContent = firstAlarm.getString("issueContent");
            }
            Weathernow weathernow = new Weathernow(
                    now.getString("cityname"), // 城市名称
                    now.getString("date"), // 日期
                    now.getString("temp"), // 当前温度
                    now.getString("weather"), // 天气状况
                    now.getString("WD"), // 风向
                    now.getString("WS"), // 风力
                    now.getString("SD"), // 湿度
                    now.getString("weathercode"), // 天气代码
                    now.getString("aqi_pm25"), // AQI_PM2.5
                    issueContent, // 预警信息
                    hour3List, // 设置 Weatherhour3 列表
                    day2and15List, // 设置 WeatherDay2and15 列表
                    lifeIndex.getJSONObject("紫外线指数").getString("state"), // 紫外线指数
                    lifeIndex.getJSONObject("运动指数").getString("state"), // 运动指数
                    lifeIndex.getJSONObject("感冒指数").getString("state"), // 感冒指数
                    lifeIndex.getJSONObject("洗车指数").getString("state"), // 洗车指数
                    lifeIndex.getJSONObject("过敏指数").getString("state"), // 过敏指数
                    lifeIndex.getJSONObject("穿衣指数").getString("state") // 穿衣指数
            );


            // 将解析得到的 Weathernow 对象添加到列表中
            weatherList.add(weathernow);
            // 输出 Weathernow 对象的内容到控制台
            Log.d("WeatherFragment", "City Name: " + weathernow.getAreaCn());
            Log.d("WeatherFragment", "Date: " + weathernow.getDate());
            Log.d("WeatherFragment", "Temperature: " + weathernow.getTemp());
            Log.d("WeatherFragment", "Weather: " + weathernow.getWeather());
            Log.d("WeatherFragment", "Wind Direction: " + weathernow.getWD());
            Log.d("WeatherFragment", "Wind Speed: " + weathernow.getWS());
            Log.d("WeatherFragment", "Humidity: " + weathernow.getSD());
            Log.d("WeatherFragment", "Weather Code: " + weathernow.getWeatherCode());
            Log.d("WeatherFragment", "AQI: " + weathernow.getAqi());
            Log.d("WeatherFragment", "Issue Content: " + weathernow.getIssueContent());
            Log.d("WeatherFragment", "UV Index: " + weathernow.getLifeIndexUv());
            Log.d("WeatherFragment", "Sport Index: " + weathernow.getLifeIndexSport());
            Log.d("WeatherFragment", "Cold Index: " + weathernow.getLifeIndexCold());
            Log.d("WeatherFragment", "Washcar Index: " + weathernow.getLifeIndexWashcar());
            Log.d("WeatherFragment", "Allergy Index: " + weathernow.getLifeIndexAllergy());
            Log.d("WeatherFragment", "Dress Index: " + weathernow.getLifeIndexDress());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
